let host = "http://127.0.0.1:1337";
let logipts = document.querySelectorAll(".acolog input");

let regipts = document.querySelectorAll(".acoreg input");

// 忘记密码
let foget = document.querySelector(".foget");

// 登录按钮
let loginbtn = document.querySelector(".acountmainlog");

// 注册按钮
let regbtn = document.querySelector(".acountmainreg");

// 获取登录复选框
let logck = document.getElementById("logck");

// 获取注册复选框
let regck = document.getElementById("regck");

let [LOG,REG] = ["登录","注册"];

let stat = LOG;
// 点头部的注册和登录
let reg = document.querySelector(".reg");
let logi = document.querySelector(".login");
let acreg = document.querySelector(".acountmain-reg");
let aclog = document.querySelector(".acountmain-log");
reg.onclick = function() {
    acreg.style.left = 0;
    aclog.style.left = -400+"px";
    this.classList.add("active");
    logi.classList.remove("active");
    stat = REG;
}

logi.onclick = function() {
    acreg.style.left = 400+"px";
    aclog.style.left = 0;
    this.classList.add("active");
    reg.classList.remove("active");
    stat = LOG;
}


loginbtn.onclick = async function(e) {
    e.preventDefault();
    let regUrl = host + "/api/v1/auth/login";
    let data = {userName:logipts[0].value,password:logipts[1].value};
    let {userName,password} =  data;
    console.log(data);
    if(userName.trim()=="" || password.trim()=="") {
        alert("请输入用户名或者密码");
    }else if(logck.checked==false)  {
           alert("请勾选同意按钮");
    }else {
        let p1 = await axios.post(regUrl,data);
        console.log(p1.data.data);
        if(p1.data.code==1) {
            console.log("登录成功");
            alert("登录成功");
            location.href = "index.html";
            localStorage.setItem("token",p1.data.data);
            localStorage.setItem("username",userName);
        } else {
            console.log("登录失败");
        }
    }
}

regbtn.onclick = async function(e) {
    e.preventDefault();
    let regUrl = host + "/api/v1/auth/reg";
    let data = {userName:regipts[0].value,password:regipts[1].value};
    let {userName,password} =  data;
    if(userName.trim()=="" || password.trim()=="") {
        alert("请输入用户名或者密码");
    } else if(regck.checked==false)  {
          alert("请勾选同意按钮");
      }
    else {
        let p1 = await axios.post(regUrl,data);
        // console.log(p1.data.data);
        if(p1.data.code==1) {
            alert("注册成功");
        } else {
            alert("注册失败");
            // console.log("注册失败");
        }
    }
}

foget.onclick = function() {
      window.open("alterPassword.html");
}


changeColor(logipts,loginbtn);

changeColor(regipts,regbtn);


 